/**
 * Title: AreaManager.java
 * Description:
 * Copyright: Copyright (c) 2008
 * Company:深圳彩讯科技有限公司
 * Author 罗旭东 (hi@luoxudong.com)
 * Date 2014-1-8 下午3:59:20
 * Version 1.0
 */
package com.luoxudong.phonenumber.logic.area.manager;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;

import com.google.gson.Gson;
import com.luoxudong.phonenumber.logic.area.interfaces.IAreaManager;
import com.luoxudong.phonenumber.logic.area.interfaces.IGetAllAreaInfoListener;
import com.luoxudong.phonenumber.logic.common.BaseConfig;
import com.luoxudong.phonenumber.logic.db.dao.AreaDao;
import com.luoxudong.phonenumber.logic.db.model.AreaInfo;
import com.luoxudong.phonenumber.logic.http.model.request.GetAllAreaInfoReq;
import com.luoxudong.phonenumber.logic.http.model.response.GetAllAreaInfoRsp;
import com.luoxudong.phonenumber.logic.http.model.response.GetAllAreaInfoRsp.CityItem;
import com.luoxudong.phonenumber.logic.http.model.response.GetAllAreaInfoRsp.ProvinceItem;
import com.luoxudong.phonenumber.logic.http.model.response.GetAllProvincesRsp;
import com.luoxudong.phonenumber.logic.utils.ConfigUtil;
import com.luoxudong.phonenumber.service.database.DaoManagerFactory;
import com.luoxudong.phonenumber.service.net.http.asynchttp.AsyncHttpUtil;
import com.luoxudong.phonenumber.service.net.http.asynchttp.interfaces.ISimpleJsonRequestListener;
import com.luoxudong.phonenumber.service.net.http.utils.HttpApiUtil;
import com.luoxudong.phonenumber.service.utils.FileUtil;

/** 
 * ClassName: AreaManager
 * Description:TODO(这里用一句话描述这个类的作用)
 * Create by 罗旭东
 * Date 2014-1-8 下午3:59:20
 */
public class AreaManager implements IAreaManager{
	private AreaDao areaDao = null;
	
	private void checkRemoteFileDao()
	{
		if (areaDao == null)
		{
			areaDao = DaoManagerFactory.getDaoManager(BaseConfig.DATABASE_PATH).getDataHelper(AreaDao.class, AreaInfo.class);
		}
	}
	
	@Override
	public void initLocalAreaInfo(Context context) {
		String LastAreaUpdateTime = ConfigUtil.getLastAreaUpdateTime();
		
		initLocalArea(context);
		getAllAreaInfoFromService(context, LastAreaUpdateTime, new IGetAllAreaInfoListener() {
			
			@Override
			public void onCallbackSuc(GetAllAreaInfoRsp rsp) {
				if (rsp != null)
				{
					List<ProvinceItem> provinceItems = rsp.getResultList();
					
					if (provinceItems != null)
					{
						List<AreaInfo> areaInfos = getAreaInfosByProvinces(provinceItems);
						checkRemoteFileDao();
						areaDao.deleteAllData();
						try {
							areaDao.batchInsert(areaInfos);
						} catch (Exception e) {
						}
						
						ConfigUtil.setLastAreaUpdateTime(rsp.getUpdateTime());
						
					}
					
				}
			}
			
			@Override
			public void onCallbackFail(int errorCode, String errorMsg) {
				
			}
		});
		
		//getAllProvincesFromService(context, null);
	}

	@Override
	public void getAllAreaInfoFromService(Context context, String lastUpdateTime, final IGetAllAreaInfoListener listener) {
		GetAllAreaInfoReq req = new GetAllAreaInfoReq();
		req.setLastUpdateTime(lastUpdateTime);
		
		AsyncHttpUtil.simpleJsonRequest(HttpApiUtil.getFullUrl(context, HttpApiUtil.URL_SERVICE_GET_ALL_AREAS), req, GetAllAreaInfoRsp.class, new ISimpleJsonRequestListener<GetAllAreaInfoRsp>() {

			@Override
			public void onFailed(int errorCode, String errorMsg) {
				if (listener != null)
				{
					listener.onCallbackFail(errorCode, errorMsg);
				}
			}

			@Override
			public void onStart() {
				
			}

			@Override
			public void onSuccess(GetAllAreaInfoRsp responseInfo) {
				if (listener != null)
				{
					listener.onCallbackSuc(responseInfo);
				}
			}
		});
	}
	
	@Override
	public void getAllProvincesFromService(Context context, IGetAllAreaInfoListener listener) {
		AsyncHttpUtil.simpleJsonRequest(HttpApiUtil.getFullUrl(context, HttpApiUtil.URL_SERVICE_GET_PROVINCE), null, GetAllProvincesRsp.class, new ISimpleJsonRequestListener<GetAllProvincesRsp>() {

			@Override
			public void onFailed(int errorCode, String errorMsg) {
				
			}

			@Override
			public void onStart() {
				
			}

			@Override
			public void onSuccess(GetAllProvincesRsp responseInfo) {
				
			}
		});
	}
	
	@Override
	public List<AreaInfo> getAreaInfoByParentId(int parentId) {
		checkRemoteFileDao();
		return areaDao.getAreaInfoByParentId(parentId);
	}
	
	@Override
	public long getAreaInfoCountByParentId(int parentId) {
		checkRemoteFileDao();
		return areaDao.getAreaInfoCountByParentId(parentId);
	}
	
	@Override
	public AreaInfo getAreaInfoByAreaId(int areaId) {
		checkRemoteFileDao();
		return areaDao.getAreaInfoByAreaId(areaId);
	}
	
	private void initLocalArea(Context context)
	{
		checkRemoteFileDao();
		long num = areaDao.queryCount();
		
		if (num <= 0)//初始化
		{
			String areaGson = FileUtil.getFromAssets(context, "area.txt");
			Gson gson = new Gson();
			GetAllAreaInfoRsp allAreaInfo = gson.fromJson(areaGson, GetAllAreaInfoRsp.class);
			
			List<ProvinceItem> provinceItems = null;
			if (allAreaInfo != null && (provinceItems = allAreaInfo.getResultList()) != null)
			{
				List<AreaInfo> areaInfos = getAreaInfosByProvinces(provinceItems);
				try {
					areaDao.batchInsert(areaInfos);
				} catch (Exception e) {
				}
			}
		}
	}
	
	private List<AreaInfo> getAreaInfosByProvinces(List<ProvinceItem> provinceItems)
	{
		List<AreaInfo> areaInfos = new ArrayList<AreaInfo>();
		AreaInfo areaInfo = null;
		for (ProvinceItem provinceItem : provinceItems)
		{
			areaInfo = new AreaInfo();
			areaInfo.setAreaId(provinceItem.getProvinceId());
			areaInfo.setAreaName(provinceItem.getProvinceName());
			areaInfos.add(areaInfo);
			
			List<CityItem> cityItems = provinceItem.getCityList();
			
			if (cityItems != null)
			{
				for (CityItem cityItem : cityItems)
				{
					areaInfo = new AreaInfo();
					areaInfo.setAreaId(cityItem.getCityId());
					areaInfo.setAreaName(cityItem.getCityName());
					areaInfo.setParentAreaId(provinceItem.getProvinceId());
					areaInfos.add(areaInfo);
				}
			}
		}
		
		return areaInfos;
	}
	
}
